﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class LocationAppointmentListControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }
        protected void btnAddAppointmentToLocation_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int companyid = userIdentity.RSCompanyId;
                int locationid = hfLocationId.Value == "" ? 0 : Convert.ToInt32(hfLocationId.Value);
                if (locationid > 0)
                    ucModalManageAppointmentControl.ShowModal(companyid, locationid, 0);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);


        }

        protected void gvAppointments_RowDeleting(Object sender, GridViewDeleteEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int appointmentid = Convert.ToInt32(gvLocationAppointment.DataKeys[e.RowIndex].Value);
                int locationid = hfLocationId.Value == "" ? 0 : Convert.ToInt32(hfLocationId.Value);
                if (locationid == 0)
                    locationid = RSSBMCommonFunctions.GetLocationIDByAppointmentID(companyid, appointmentid);

                RemoveAppointment(companyid, locationid, appointmentid);
                ucModalActionMessageControl.ShowModal("Appointment deleted successfully!", Request.RawUrl, true);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        private void RemoveAppointment(int companyid, int locationid, int appointmentid)
        {
            if (companyid > 0 && locationid > 0 && appointmentid > 0)
            {
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@locationid", SqlDbType.Int, 0, ParameterDirection.Input, locationid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@appointmentid", SqlDbType.Int, 0, ParameterDirection.Input, appointmentid);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblAppointment WHERE AppointmentId=@appointmentId AND LocationId=@locationid AND  CompanyId=@companyid");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                //Response.Redirect(Request.RawUrl);
            }
        }

        
        protected void gvAppointments_SelectedIndexChanged(Object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int appointmentid = Convert.ToInt32(gvLocationAppointment.SelectedValue.ToString());
            int locationid = hfLocationId.Value == "" ? 0 : Convert.ToInt32(hfLocationId.Value);
            if (locationid == 0)
                locationid = RSSBMCommonFunctions.GetLocationIDByAppointmentID(companyid, appointmentid);
            ucModalManageAppointmentControl.ShowModal(companyid, locationid, appointmentid);
        }
        public void LoadAppointmentDataByID(int companyid, int locationid)
        {
            lblAppointment.Text = "Location Scheduled Visit History";
            if (companyid > 0 && locationid > 0)
            {
                hfLocationId.Value = locationid.ToString();
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;

                String sqlSELECT = "SELECT AppointmentId, Subject,[Start],[End],Description,Response,Location,AssignTo as EmployeeName,(select t.typename from rs_tbltype t where t.typeid=AppointmentTypeID) as AppointmentType,Status,LocationId FROM [rs_tblAppointment] WHERE CompanyId=@CompanyID AND LocationId=@LocationID ORDER BY Start DESC";

                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sqlSELECT, myConnection);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@locationid";
                param.Value = locationid;
                myCommand.Parameters.Add(param);

                SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
                myConnection.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                gvLocationAppointment.DataSource = ds;
                gvLocationAppointment.DataBind();
                myConnection.Close();

                if (gvLocationAppointment.Rows.Count == 0)
                {
                    //divAppointmentInfo.Visible = false;
                }
            }
        }
        public void LoadAppointmentDataByAll(int companyid)
        {
            btnAddAppointment.Visible = false;
            lblAppointment.Text = "Service Visits";
            if (companyid > 0)
            {
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;

                String sqlSELECT = "SELECT AppointmentId, Subject,[Start],[End],Description,Response,Location,AssignTo as EmployeeName,(select t.typename from rs_tbltype t where t.typeid=AppointmentTypeID) as AppointmentType,Status,LocationId FROM [rs_tblAppointment] WHERE CompanyId=@CompanyID ORDER BY Start DESC";

                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sqlSELECT, myConnection);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
                myConnection.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                gvLocationAppointment.DataSource = ds;
                gvLocationAppointment.DataBind();
                myConnection.Close();

                if (gvLocationAppointment.Rows.Count == 0)
                {
                    //divAppointmentInfo.Visible = false;
                }
            }
        }
    }
}